﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace FrameworkXNA.Scenes
{
    class Exercicio : SceneBase
    {
        Object2D bola, chao;

        float velocidade, gravidade = 1;

        public override void start()
        {
            bola = new Object2D(Game1.instance.Content.Load<Texture2D>("bola"));
            bola.x = Game1.instance.graphics.PreferredBackBufferWidth / 2;
            bola.y = 100;

            chao = new Object2D(Game1.instance.Content.Load<Texture2D>("chao"));
            chao.x = Game1.instance.graphics.PreferredBackBufferWidth / 2;
            chao.y = Game1.instance.graphics.PreferredBackBufferHeight - chao.height / 2;
            chao.setWidth(Game1.instance.graphics.PreferredBackBufferWidth);
            base.start();
        }

        public override void update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            velocidade += gravidade;
            bola.y += velocidade;

            if (bola.hitTestObject(chao))
            {
                velocidade *= -1;
                bola.y = chao.y - chao.height / 2 - bola.height / 2;
            }

            if (Controller.keyDown(Keys.D))
                bola.x += 5;

            if (Controller.keyDown(Keys.A))
                bola.x -= 5;

            if (bola.x > Game1.instance.graphics.PreferredBackBufferWidth - bola.width /2)
                bola.x = Game1.instance.graphics.PreferredBackBufferWidth - bola.width /2;

            if (bola.x <= bola.width / 2)
                bola.x = bola.width / 2;

            base.update(gameTime);
        }

        public override void draw(SpriteBatch spriteBatch)
        {
            bola.Draw(spriteBatch);
            chao.Draw(spriteBatch);
            base.draw(spriteBatch);
        }
    }
}
